package com.net.xpay.core.service.impl.safePay;

import com.net.xpay.common.domain.SafePayBusinessAuthAuditRecord;
import com.net.xpay.common.domain.User;
import com.net.xpay.common.manager.SafePayBusinessAuthAuditRecordManager;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.xpay.core.service.safePay.SafePayBookAccountService;
import com.net.xpay.core.validator.SafePayBookAccountSubmitBusinessAuthValidator;
import com.net.common.exception.BusinessException;
import com.net.common.util.LocalDateUtil;
import com.net.xpay.core.domain.safePay.SafePayBookAccount;
import com.net.xpay.core.enums.SafePayBookAccountAuditStatus;
import com.net.xpay.core.manager.safePay.SafePayBatchManager;
import com.net.xpay.core.manager.safePay.SafePayBookAccountManager;
import com.net.xpay.core.model.SafePayBookAccountSubmitBusinessAuthModel;
import com.net.xpay.core.model.SafePayUserTransLimitModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Slf4j
@Service
public class SafePayBookAccountServiceImpl implements SafePayBookAccountService {

    @Autowired
    private SafePayBatchManager safePayBatchManager;

    @Autowired
    private SafePayBookAccountManager safePayBookAccountManager;

    @Autowired
    private SafePayBusinessAuthAuditRecordManager safePayBusinessAuthAuditRecordManager;

    @Autowired
    private SafePayBookAccountSubmitBusinessAuthValidator safePayBookAccountSubmitBusinessAuthValidator;

    @Override
    public SafePayUserTransLimitModel getTransferLimit(User merchant, Long safePayBookAccountId) {
        // 获取当前用户的userId和支付宝主账号id
        Long userId = merchant.getId();
        // 首先获取当前的时间
        LocalDateTime day = LocalDateTime.now();
        // 根据当前时间获取改天的起始时间和结束时间
        LocalDateTime currentDayStartTime = LocalDateUtil.getDayStart(day);
        LocalDateTime currentDayEndTime = LocalDateUtil.getDayEnd(day);
        // 根据当前时间获取该月的起始时间和结束时间
        LocalDateTime currentMonthStartTime = LocalDateUtil.getMonthStartDay(day);
        LocalDateTime currentMonthEndTime = LocalDateUtil.getMonthEndDay(day);
        // 进行查询
        BigDecimal currentDayLimit = safePayBatchManager.getTransferLimit(currentDayStartTime, currentDayEndTime, userId, safePayBookAccountId);
        BigDecimal currentMonthLimit = safePayBatchManager.getTransferLimit(currentMonthStartTime, currentMonthEndTime, userId, safePayBookAccountId);

        return new SafePayUserTransLimitModel(currentDayLimit,currentMonthLimit);
    }

    @Override
    public SafePayBusinessAuthAuditRecord submitBusinessAuthentication(User merchant, SafePayBookAccountSubmitBusinessAuthModel model) {
        safePayBookAccountSubmitBusinessAuthValidator.validateSafePayBookAccountSubmitBusinessAuth(model);
        // 获取当前用户的userId和支付宝主账号id
        Long userId = merchant.getId();
        //更新
        SafePayBookAccount safePayBookAccount = safePayBookAccountManager.findByAlipayNickName(userId, model.getAlipayNickName());
        if (safePayBookAccount == null){
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL,"企业支付宝不存在");
        }

        SafePayBookAccountAuditStatus auditStatus = safePayBookAccount.getAuditStatus();
        //判断是否能修改提交信息
        if (auditStatus != SafePayBookAccountAuditStatus.INIT && auditStatus != SafePayBookAccountAuditStatus.FAIL){
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL,"实名认证信息已提交，请勿重复提交");
        }

        SafePayBusinessAuthAuditRecord record = safePayBusinessAuthAuditRecordManager.findBySafePayBookAccountId(safePayBookAccount.getId());
        if (record == null){
            record = new SafePayBusinessAuthAuditRecord();
            record.setSafePayBookAccountId(safePayBookAccount.getId());
            record.setAlipayNickName(safePayBookAccount.getAlipayNickName());
            record.setAlipayUid(safePayBookAccount.getAlipayUid());
            record.setUserId(safePayBookAccount.getUserId());
            record.setBusinessName(model.getBusinessName());
            record.setIdCardFront(model.getIdCardFront());
            record.setBizLicense(model.getBizLicense());
            record.setBusinessLicense(model.getBusinessLicense());
            record.setOfficeImages(model.getOfficeImages());
            record.setAuditStatus(SafePayBookAccountAuditStatus.AUDITING);
            record.setAuditSubmitTime(LocalDateTime.now());

            record.setIdCardBack(model.getIdCardBack());
            record.setIcpInfo(model.getIcpInfo());
            record.setBizSceneDesc(model.getBizSceneDesc());
            record.setMerchantCommitment(model.getMerchantCommitment());

            record.setOtherOneImages(model.getOtherOneImages());
            record.setOtherTwoImages(model.getOtherTwoImages());
            record.setOtherThreeImages(model.getOtherThreeImages());

            safePayBusinessAuthAuditRecordManager.save(record);
        }else {
            record.setSafePayBookAccountId(safePayBookAccount.getId());
            record.setAlipayNickName(safePayBookAccount.getAlipayNickName());
            record.setAlipayUid(safePayBookAccount.getAlipayUid());
            record.setUserId(safePayBookAccount.getUserId());
            record.setBusinessName(model.getBusinessName());
            record.setIdCardFront(model.getIdCardFront());
            record.setBizLicense(model.getBizLicense());
            record.setBusinessLicense(model.getBusinessLicense());
            record.setOfficeImages(model.getOfficeImages());
            record.setAuditStatus(SafePayBookAccountAuditStatus.AUDITING);
            record.setAuditSubmitTime(LocalDateTime.now());

            record.setIdCardBack(model.getIdCardBack());
            record.setIcpInfo(model.getIcpInfo());
            record.setBizSceneDesc(model.getBizSceneDesc());
            record.setMerchantCommitment(model.getMerchantCommitment());

            record.setOtherOneImages(model.getOtherOneImages());
            record.setOtherTwoImages(model.getOtherTwoImages());
            record.setOtherThreeImages(model.getOtherThreeImages());
            safePayBusinessAuthAuditRecordManager.update(record);
        }


        safePayBookAccount.setBusinessName(model.getBusinessName());
        safePayBookAccount.setIdCardFront(model.getIdCardFront());
        safePayBookAccount.setBusinessLicense(model.getBusinessLicense());
        safePayBookAccount.setOfficeImages(model.getOfficeImages());
        safePayBookAccount.setAuditStatus(SafePayBookAccountAuditStatus.AUDITING);
        safePayBookAccount.setAuditSubmitTime(LocalDateTime.now());

        safePayBookAccount.setIdCardBack(model.getIdCardBack());
        safePayBookAccount.setIcpInfo(model.getIcpInfo());
        safePayBookAccount.setBizSceneDesc(model.getBizSceneDesc());
        safePayBookAccount.setMerchantCommitment(model.getMerchantCommitment());

        safePayBookAccountManager.updateByPrimaryKeyWithVersion(safePayBookAccount);

        return record;
    }

    @Override
    public void updateSafePayBookAccount(User merchant, Long id) {
         if (id == 0){
             throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL,"修改实名认证异常");
         }
        Long userId = merchant.getId();
        SafePayBookAccount safePayBookAccount = safePayBookAccountManager.findById(userId, id);
        if (safePayBookAccount == null){
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL,"用户不存在");
        }

        safePayBookAccount.setAuditStatus(SafePayBookAccountAuditStatus.INIT);
        safePayBookAccountManager.updateByPrimaryKeyWithVersion(safePayBookAccount);

        SafePayBusinessAuthAuditRecord safePayBusinessAuthAuditRecord = safePayBusinessAuthAuditRecordManager.findBySafePayBookAccountId(safePayBookAccount.getId());
        safePayBusinessAuthAuditRecord.setAuditStatus(SafePayBookAccountAuditStatus.INIT);
        safePayBusinessAuthAuditRecordManager.update(safePayBusinessAuthAuditRecord);


    }
}
